package com.wit.dao.impl;

import hibernate.HibernateSessionFactory;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wit.dao.AgendaDao;
import com.wit.po.Adgenda;
import com.wit.po.Notice;

public class AgendaDaoImpl implements AgendaDao {

	public boolean addAgenda(Adgenda agenda) {

		Session session = null;
		boolean flag = false;
		try{
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			session.save(agenda);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}
	
	
	public Adgenda getAgendaById(Integer agendaId)
	{
		if(agendaId == null)
			return null;
		Session session = null;
		Adgenda agenda = null;
		try{
			
			session = HibernateSessionFactory.getSession();
			String hql = "from Adgenda agenda where agenda.agendaId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, agendaId);
			
			agenda = (Adgenda)query.uniqueResult();
			
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			if(session != null && session.isOpen())
				session.close();
		}
		return agenda;
	}

	
	public List<Adgenda> getAgendaList(String employeeId, String date) {

		if(employeeId == null || date == null)
			return null;
		Session session = null;
		List<Adgenda> agendaList = null;
		try{
			session = HibernateSessionFactory.getSession();
			
			String hql = "from Adgenda agenda where agenda.employeeId=? and DATE(agenda.agendaStart)=?";
			Query query = session.createQuery(hql);
			
			query.setString(0, employeeId);
			query.setString(1, date);
			
			agendaList = (List<Adgenda>)query.list();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null && session.isOpen())
				session.close();
		}
		return agendaList;
	}

	
	public boolean removeAgenda(Adgenda agenda) {

		boolean flag = false;
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.delete(agenda);
			
			tx.commit();
			flag = true;
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	
	public boolean updateAgenda(Adgenda agenda) {
		
		Session session = null;
		boolean flag = false;
		try {
		session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		
		session.update(agenda);
		
		tx.commit();
		flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

}
